import router from "./index";
import store from '@/store';
import {getStorage} from 'hcg-storage';

router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title
	}
	let isLogin = store.state.user.isLogin;
	if (to.meta.login && !isLogin) {
		let token = store.state.user.token || getStorage('token');
		if (token) {
			store.dispatch('getUserDetail', token).then(() => {
				next();
			})
		} else {
			next({
				path: '/login',
				query: {
					redirect: to.fullPath
				}
			})
		}
	} else {
		if (isLogin && to.query.redirect) {
			next('/');
		} else {
			next();
		}
	}
});
